package com.winning.joiner.magiccube.common.utils;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import org.hibernate.validator.HibernateValidator;

import com.winning.joiner.magiccube.common.domain.BaseEntity;
import com.winning.joiner.magiccube.common.exception.file.FileldPatternException;

public class ValidatorUtils {
	
	private static Validator validator = Validation
            .byProvider(HibernateValidator.class).configure().failFast(true).buildValidatorFactory().getValidator();

    /**
     * 功能描述: <br>
     * 〈注解验证参数〉
     *
     * @param obj
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static <T> void validate(T obj) {
        Set<ConstraintViolation<T>> constraintViolations = validator.validate(obj);
        // 抛出检验异常
        if (constraintViolations.size() > 0) {
        	throw new FileldPatternException( constraintViolations.iterator().next().getMessage());
        }
    }
    
    public static void main(String[] args) {
    	System.out.println("start----------");
    	BaseEntity baseEntity=new BaseEntity();
    	baseEntity.setSearchValue("hello");
    	baseEntity.setCreateBy("12313245");
    	validate(baseEntity);
	}

}
